using System;

namespace OracleInternal.Json.Binary
{
	internal sealed class ByteBuffer
	{
		private int m_pos;

		private int m_bufferLen;

		private byte[] m_buffer;

		private byte[] m_bytesForInt;

		private byte[] m_bytesForShort;

		internal static ByteBuffer Wrap(byte[] buffer)
		{
			return new ByteBuffer(buffer);
		}

		internal ByteBuffer(byte[] buffer)
		{
			m_buffer = buffer;
			m_bufferLen = m_buffer.Length;
			m_bytesForInt = new byte[4];
			m_bytesForShort = new byte[2];
		}

		internal int GetInt()
		{
			int @int = GetInt(m_pos);
			m_pos += 4;
			return @int;
		}

		internal int GetInt(int pos)
		{
			if (pos + 4 > m_bufferLen)
			{
				throw new IndexOutOfRangeException();
			}
			if (BitConverter.IsLittleEndian)
			{
				int num = 0;
				for (int num2 = m_bytesForInt.Length - 1; num2 >= 0; num2--)
				{
					m_bytesForInt[num] = m_buffer[pos + num2];
					num++;
				}
				return BitConverter.ToInt32(m_bytesForInt, 0);
			}
			return BitConverter.ToInt32(m_buffer, pos);
		}

		internal int Position()
		{
			return m_pos;
		}

		internal int Position(int pos)
		{
			m_pos = pos;
			return m_pos;
		}

		internal int Remaining()
		{
			return m_buffer.Length - m_pos;
		}

		internal int Limit()
		{
			return m_bufferLen;
		}

		internal byte Get()
		{
			return m_buffer[m_pos++];
		}

		internal byte Get(int pos)
		{
			return m_buffer[pos];
		}

		internal short GetShort()
		{
			short @short = GetShort(m_pos);
			m_pos += 2;
			return @short;
		}

		internal short GetShort(int pos)
		{
			if (pos + 2 > m_bufferLen)
			{
				throw new IndexOutOfRangeException();
			}
			if (BitConverter.IsLittleEndian)
			{
				System.Array.Copy(m_buffer, pos, m_bytesForShort, 0, 2);
				System.Array.Reverse(m_bytesForShort);
				return BitConverter.ToInt16(m_bytesForShort, 0);
			}
			return BitConverter.ToInt16(m_buffer, pos);
		}

		internal byte[] Array()
		{
			return m_buffer;
		}

		internal ByteBuffer Get(byte[] dst)
		{
			System.Array.Copy(m_buffer, m_pos, dst, 0, dst.Length);
			return this;
		}
	}
}
